import { startcCanvasSim } from '@renderer/store/canvas/canvasSim'
import {
  mainAddEdge,
  mainAddNode,
  mainChangeCanvasProperties,
  mainChangeEdgeProperties,
  mainChangeNodePorts,
  mainChangeNodePosition,
  mainChangeNodeProperties,
  mainChangeNodeSize,
  mainClearCanvas,
  mainDeleteEdge,
  mainDeleteNode,
  mainRewriteCanvas,
  mainSelectCell
} from '@renderer/store/canvas/mainProcessData'
import { useMessageStore } from '@renderer/store/message/message'
import { useLayerStore } from '@renderer/store/resourceBar/layer'

export const mainProcess = (): void => {
  //监听画布数据变化
  window.electron.ipcRenderer.on('canvasData', (e, data): void => {
    const { methodName, callerSource, result } = data
    switch (methodName) {
      case 'setCanvas':
        mainRewriteCanvas(result)
        useLayerStore().canvasAddLayer(result.nodes)
        useLayerStore().canvasAddLayer(result.edges)
        break
      case 'clearCanvas':
        mainClearCanvas()
        useLayerStore().clear()
        break
      case 'addNodes':
        mainAddNode(result)
        useLayerStore().canvasAddLayer(result)
        break
      case 'deleteNodes':
        mainDeleteNode(result)
        useLayerStore().deleteNode(result)
        break
      case 'updateNodes':
        mainChangeNodeProperties(result)
        break
      case 'moveNode':
        mainChangeNodePosition(result)
        break
      case 'changeNodeSize':
        mainChangeNodeSize(result)
        break
      case 'changeNodePort':
        mainChangeNodePorts(result)
        break
      case 'addEdges':
        mainAddEdge(result)
        useLayerStore().canvasAddLayer(result)
        break
      case 'updateEdges':
        mainChangeEdgeProperties(result)
        break
      case 'deleteEdges':
        mainDeleteEdge(result)
        useLayerStore().deleteNode(result)
        break
      case 'setCanvasProperties':
        mainChangeCanvasProperties(result)
        break
      case 'selectNodesOrEdges':
        mainSelectCell(result)
        if (callerSource !== 'resourceMana') {
          useLayerStore().selectNodesByMain(result)
        }
        break
      default:
        break
    }
  })

  //监听主进程发送的消息数据
  window.electron.ipcRenderer.on('mainProcessMsg', (e, data): void => {
    useMessageStore().addPrompt(data)
  })

  //监听仿真消息
  window.electron.ipcRenderer.on('simulationData', (e, data): void => {
    startcCanvasSim(data)
  })

  //监听资源栏消息
  window.electron.ipcRenderer.on('resourceChangeNodeName', (e, data): void => {
    useLayerStore().mainUpdateNameToLayer(data)
  })
}
